package com.ruoyi.web.controller.system.fuw;

import java.io.IOException;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.web.controller.util.DataUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fuw.domain.FuwPerson;
import com.ruoyi.fuw.service.IFuwPersonService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 用户信息Controller
 * 
 * @author ruoyi
 * @date 2021-10-09
 */
@RestController
@RequestMapping("/fuw/person")
public class FuwPersonController extends BaseController
{
    @Autowired
    private IFuwPersonService fuwPersonService;

    /**
     * 查询用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:person:list')")
    @GetMapping("/list")
    public TableDataInfo list(FuwPerson fuwPerson)
    {
        startPage();
        List<FuwPerson> list = fuwPersonService.selectFuwPersonList(fuwPerson);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @PreAuthorize("@ss.hasPermi('fuw:person:export')")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FuwPerson fuwPerson)
    {
        List<FuwPerson> list = fuwPersonService.selectFuwPersonList(fuwPerson);
        ExcelUtil<FuwPerson> util = new ExcelUtil<FuwPerson>(FuwPerson.class);
        return util.exportExcel(list, "用户信息数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:person:query')")
    @GetMapping(value = "/{personId}")
    public AjaxResult getInfo(@PathVariable("personId") String personId)
    {
        return AjaxResult.success(fuwPersonService.selectFuwPersonByPersonId(personId));
    }

    /**
     * 新增用户信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:person:add')")
    @Log(title = "用户信息", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResult add(MultipartFile file,
                          @RequestParam("personId") String personId,
                          @RequestParam("personUname") String personUname,
                          @RequestParam("personName") String personName,
                          @RequestParam("personGender") Integer personGender,
                          @RequestParam("personAge") String personAge,
                          @RequestParam("personTel") String personTel,
                          @RequestParam("personNumber") String personNumber,
                          @RequestParam("personUtext") String personUtext,
                          @RequestParam("personAddress") String personAddress)
    {
        // 根据文件路径上传
        String fullPath = "";
        if(file != null){
            try {
                fullPath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            fullPath = "http://localhost/dev-api" + fullPath;
        }

//        // 随机生成 person_id 作为唯一主键
//        UUID uuid=UUID.randomUUID();
//        String uuidStr=uuid.toString();

        // 自定义格式UI
        String uuidStr = DataUtils.fuwUUID("Per");

        // 定义类
        FuwPerson fuwPerson = new FuwPerson();
        fuwPerson.setPersonId(uuidStr);
        fuwPerson.setPersonUname(personUname);
        fuwPerson.setPersonUphoto(fullPath);
        fuwPerson.setPersonName(personName);
        fuwPerson.setPersonGender(personGender);
        fuwPerson.setPersonAge(personAge);
        fuwPerson.setPersonTel(personTel);
        fuwPerson.setPersonNumber(personNumber);
        fuwPerson.setPersonUtext(personUtext);
        fuwPerson.setPersonAddress(personAddress);

        return toAjax(fuwPersonService.insertFuwPerson(fuwPerson));
    }

    /**
     * 修改用户信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:person:edit')")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
    public AjaxResult edit(@RequestParam("fileNow") Boolean fileNow,
                           MultipartFile file,
                           @RequestParam("personId") String personId,
                           @RequestParam("personUname") String personUname,
                           @RequestParam("personName") String personName,
                           @RequestParam("personGender") Integer personGender,
                           @RequestParam("personAge") String personAge,
                           @RequestParam("personTel") String personTel,
                           @RequestParam("personNumber") String personNumber,
                           @RequestParam("personUtext") String personUtext,
                           @RequestParam("personAddress") String personAddress)
    {
        // 根据文件路径上传
        String fullPath = "";
        if(file != null){
            try {
                fullPath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), file);
            } catch (IOException e) {
                e.printStackTrace();
            }
            fullPath = "http://localhost/dev-api" + fullPath;
        }

        // 定义类
        FuwPerson fuwPerson = fuwPersonService.selectFuwPersonByPersonId(personId);

        fuwPerson.setPersonUphoto(fullPath);

        if(fileNow){
            fuwPerson.setPersonUphoto(null);
        }

        // 定义类
        fuwPerson.setPersonUphoto(fullPath);
        fuwPerson.setPersonUname(personUname);
        fuwPerson.setPersonGender(personGender);
        fuwPerson.setPersonName(personName);
        fuwPerson.setPersonAge(personAge);
        fuwPerson.setPersonTel(personTel);
        fuwPerson.setPersonNumber(personNumber);
        fuwPerson.setPersonUtext(personUtext);
        fuwPerson.setPersonAddress(personAddress);

        return toAjax(fuwPersonService.updateFuwPerson(fuwPerson));
    }

    /**
     * 删除用户信息
     */
    @PreAuthorize("@ss.hasPermi('fuw:person:remove')")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{personIds}")
    public AjaxResult remove(@PathVariable String[] personIds)
    {
        return toAjax(fuwPersonService.deleteFuwPersonByPersonIds(personIds));
    }
}
